package com.example.controller.pc;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.result.Result;
import com.example.entity.dto.ZPX.GongDanChuZhanDto;
import com.example.entity.dto.ZPX.GongDanJinZhanDto;
import com.example.entity.dto.ZPX.MoctaShowDto;
import com.example.entity.hz.IHead;
import com.example.entity.mysql.Resource;
import com.example.entity.vo.ZPX.YpjSfctaItemVo;
import com.example.entity.vo.ZPX.YpjShowVo;
import com.example.entity.yifei.Mocty;
import com.example.enums.MOCStatusEnum;
import com.example.exception.CustomException;
import com.example.mapper.*;
import com.example.mapper.pc.*;
import com.example.service.*;
import com.example.service.pc.*;
import com.example.untils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/api/zpx")
@Api(tags = "装配车间接口")
public class ZPController {

    @Autowired
    private IBodyService iBodyService;
    @Autowired
    private ZnxService znxService;
    @Autowired
    private MaintainplanMapper maintainplanMapper;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private ProductprocessresourceService productprocessresourceService;
    @Autowired
    private ResourcegroupService resourcegroupService;
    @Autowired
    private WorkresourceService workresourceService;
    @Autowired
    private WorkresourceMapper workresourceMapper;
    @Autowired
    private MoctyMapper moctyMapper;
    @Autowired
    private MoctaService moctaService;
    @Autowired
    private CoptdMapper coptdMapper;
    @Autowired
    private MoctaMapper moctaMapper;
    @Autowired
    private MacstatuslogService macstatuslogService;
    @Autowired
    private QmsmgMapper qmsmgMapper;
    @Autowired
    private SFCTEMapper sfcteMapper;
    @Autowired
    private MoclogService moclogService;
    @Autowired
    private IHeadService iHeadService;
    @Autowired
    private QmsmgService qmsmgService;
    @Autowired
    private MocworklogService mocworklogService;
    @Autowired
    private MocworklogMapper mocworklogMapper;
    @Autowired
    private ScheduleconfigService scheduleconfigService;
    @Autowired
    private SfctaMapper sfctaMapper;
    @Autowired
    private ResourcegroupMapper resourcegroupMapper;
    @Autowired
    private MochuanjilogService mochuanjilogService;
    @Autowired
    private ProductprocessresourceMapper productprocessresourceMapper;
    @Autowired
    private PersoninoutMapper personinoutMapper;
    @Autowired
    private PersoninoutService personinoutService;
    @Autowired
    private JinchuzhanlogMapper jinchuzhanlogMapper;
    @Autowired
    private JinchuzhanlogService jinchuzhanlogService;
    @Autowired
    private MaconlineinfohistoryMapper maconlineinfohistoryMapper;
    @Autowired
    private MaconlineinfohistoryService maconlineinfohistoryService;
    @Autowired
    private MaconlineinfoMapper maconlineinfoMapper;
    @Autowired
    private MaconlineinfoService maconlineinfoService;
    @Autowired
    private EqtteMapper eqtteMapper;
    @Autowired
    private EqttfMapper eqttfMapper;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private InspectiontimeMapper inspectiontimeMapper;
    @Autowired
    private ZPMapper zpMapper;

    @ApiOperation("已排机过的大工单展示")
    @PostMapping("ypjShow")
    public Result<List<YpjShowVo>> ypjShow(@RequestBody MoctaShowDto dto) {
        List<YpjShowVo> list = zpMapper.ypjShow(dto);
        List<String> ids = new ArrayList<>();
        for(YpjShowVo t : list) ids.add(t.getWorkSingle().trim() + t.getWorkNo().trim());
        List<YpjSfctaItemVo> itemVos = zpMapper.sfctaItemList(ids);

        List<Resource> resourceList = resourceMapper.selectList(new LambdaQueryWrapper<Resource>().eq(Resource::getResourceType, 2));
        Map<Integer, Resource> resourceMap = new HashMap<>();
        Map<String, Resource> resourceUdf05Map = new HashMap<>();
        for(Resource t : resourceList) {
            if(StringUtils.isNotEmpty(t.getMoctyudf05())) resourceUdf05Map.put(t.getMoctyudf05().trim(), t);
            resourceMap.put(t.getResourceGroupIdIsF(), t);
        }
        for(YpjSfctaItemVo t : itemVos) {
            if(StringUtils.isEmpty(t.getProductLineId())) continue;
            if("****".equals(t.getProductLineId().trim())) continue;
            Resource resource = resourceMap.get(Integer.parseInt(t.getProductLineId().trim()));
            if(resource == null) continue;
            t.setProductLineNo(resource.getDeviceid());
            t.setProductLineName(resource.getDevicename());

            Resource resource1 = resourceUdf05Map.get(t.getWorkSingle().trim() + t.getWorkNo().trim() + t.getProcessNo().trim());
            if(resource1 == null) continue;
            t.setJinzhanUuid(resource1.getJinzhanUuid());

            IHead iHead = iHeadService.getOne(new LambdaQueryWrapper<IHead>().eq(IHead::getMoctyudf05, t.getWorkSingle().trim() + t.getWorkNo().trim() + t.getProcessNo().trim())
                    .eq(IHead::getJinzhanUuid, resource.getJinzhanUuid()).eq(IHead::getType, "SJ"));
            if(iHead != null) t.setSjNo(iHead.getUid().toString());
        }
        Map<String, List<YpjSfctaItemVo>> map = new HashMap<>();
        for(YpjSfctaItemVo t : itemVos) {
            List<YpjSfctaItemVo> vos = map.get(t.getWorkSingle().trim() + t.getWorkNo().trim());
            if(vos == null) vos = new ArrayList<>();
            vos.add(t);
            map.put(t.getWorkSingle().trim() + t.getWorkNo().trim(), vos);
        }
        for(YpjShowVo t : list) {
            List<YpjSfctaItemVo> vos = map.get(t.getWorkSingle().trim() + t.getWorkNo().trim());
            if(vos.isEmpty()) continue;
            vos.sort(new Comparator<YpjSfctaItemVo>() {
                @Override
                public int compare(YpjSfctaItemVo o1, YpjSfctaItemVo o2) {
                    return o1.getProcessNo().compareTo(o2.getProcessNo());
                }
            });
            t.setSfctaItemList(vos);
        }
        return Result.success(list);
    }

    @ApiOperation("大工单进站")
    @PostMapping("gongDanJinZhan")
    @DSTransactional
    public Result gongDanJinZhan(@RequestBody GongDanJinZhanDto dto) {
        Mocty mocty = moctyMapper.selectOne(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, dto.getMoctyUdf05()));
        if(mocty == null) {
            throw new CustomException("500", "大工单排产数据不存在");
        }
        if(!MOCStatusEnum.DaiJinZhan.getValue().equals(mocty.getUdf01().trim())) {
            throw new CustomException("500", "大工单不是[待进站]状态");
        }
        mocty.setUdf01(MOCStatusEnum.ShengChanZhong.getValue());
        moctyMapper.update(mocty, new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, dto.getMoctyUdf05()));
        return Result.success();
    }

    @ApiOperation("大工单出站")
    @PostMapping("gongDanChuZhan")
    @DSTransactional
    public Result gongDanChuZhan(@RequestBody GongDanChuZhanDto dto) {
        Mocty mocty = moctyMapper.selectOne(new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, dto.getMoctyUdf05()));
        if(mocty == null) {
            throw new CustomException("500", "大工单排产数据不存在");
        }
        if(!MOCStatusEnum.ShengChanZhong.getValue().equals(mocty.getUdf01().trim())) {
            throw new CustomException("500", "大工单不是[生产中]状态");
        }
        List<Mocty> moctyList = moctyMapper.selectList(new LambdaQueryWrapper<Mocty>().likeRight(Mocty::getUdf05, dto.getMoctyUdf05() + "0"));
        if(moctyList.isEmpty()) {
            throw new CustomException("500", "没有任何工序.");
        }
        boolean flag = false;
        for(Mocty t : moctyList) {
            if(MOCStatusEnum.YiWanGong.getValue().equals(t.getUdf01().trim())) continue;
            flag = true;  break;
        }
        if(!flag) mocty.setUdf01(MOCStatusEnum.YiWanGong.getValue());
        else mocty.setUdf01(MOCStatusEnum.DaiJinZhan.getValue());
        moctyMapper.update(mocty, new LambdaQueryWrapper<Mocty>().eq(Mocty::getUdf05, dto.getMoctyUdf05()));
        return Result.success();
    }

}
